package com.dai.base;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.Map;

@RunWith(SpringRunner.class)
@Slf4j
@SpringBootTest
public class FlowableVariable {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
//部署
    @Test
    public void test() {
        System.err.println("我拿到了吗"+repositoryService);
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/one/FirstFlowable.bpmn20.xml")
                .name("流程变量")
                .deploy();
        System.err.println(deploy.getId()+"部署的流程id是");
    }
    //启动流程并且设置流程变量
    @Test
    public void start(){
        Map<String, Object> map = new HashMap<>();
        String startKey = "Flowable:1:ce35c495-5a57-11f0-b20d-b025aa86e2c3";
        map.put("Variable1","张三");
        map.put("Variabl2","李四");
        map.put("Variabl3","王五");
        runtimeService.startProcessInstanceById(startKey,map);
    }

    //新增全局变量和局部变量
    @Test
    public void addVariable(){
        String  runId= "9b6aed86-5b07-11f0-b666-b025aa86e2c3";
        String variables = "b20b3cad-5a5b-11f0-a589-b025aa86e2c3";
        runtimeService.setVariable(runId,"Variiable4","赵六");
        runtimeService.setVariableLocal(runId,"Variiable5","孙7");
        taskService.setVariable(variables,"Variiable6","北京");
        taskService.setVariableLocal(variables,"Variiable7","上海");

    }
//查看流程变量
    @Test
    public void query(){
        String  runId= "9b693fd2-5b07-11f0-b666-b025aa86e2c3";
        // String variables = "9b68f1ae-5b07-11f0-b666-b025aa86e2c3";
        // Map<String, Object> variables1 = taskService.getVariables(variables);
        Map<String, Object> variables2 = runtimeService.getVariables(runId);
        log.error("Run我的流程变量是{}",variables2);
        // log.error("Task我的流程变量是{}",variables1);
    }
    //完成任务，task新增的局部变量会在任务执行完成后消失
    @Test
    public void commy(){
        String key = "cf7adb9a-5a64-11f0-a64e-b025aa86e2c3";
        Map<String, Object> map = new HashMap<>();
        map.put("李四","张撒谎");
        taskService.complete(key,map);
    }

}





